// pages/setUpShop/setUpShop.js
var app = getApp();
const getPageInfoUrl = '/Goods/openShopIndex';
const getGoodsListUrl ='/Goods/openShopGoods';
const getQRCodeImgUrl = '/OpenShopQrCode/getQrCode';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    displayAuthShow: 0,//控制授权弹层的显示和隐藏
    isLoading: 1,//是否显示加载中图标
    uid:0,//当前用户的ID
    shareInfo:{},//分享所需要的信息
    adv_uri:'',//广告跳转的路径
    adv_info_url:'',//广告位图片地址
    invite_list:'',//邀请攻略列表
    QRCodeImg: '',//二维码图片地址
    QRCodeImgHttp: '',//http二维码图片地址
    isUseHttp: 0,//判断是否使用http
    isShareMaskShow:0,//控制分享弹层的显示
    page:2,//列表页码
    is_vip:0,//判断当前用户是否为VIP
    goodsList:[],//商品列表
    currentTab:0,//当前TAB切换的索引
    open_shop_invite_btn_title:'',//开通会员按钮文案
    open_shop_table_left_title:'',//tab切换左边按钮文案
    open_shop_table_right_title: '',//tab切换右边按钮文案
    open_shop_one_left_title:'',//列表左边标题
    open_shop_one_right_title:'',//列表右边标题
    open_shop_two_no_vip_title:'',//非会员列表描述
    open_shop_two_vip_title:'',//会员列表描述
    resourcesUrl:'',//静态资源文件地址
    winWidth: 0, //手机屏幕的宽度
    winHeight: 0, //手机屏幕的高度
  },
  // 免费领按钮
  freeReceive: function (e) {
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId+'&uid='+_this.data.uid,
    })
  },
  // 跳转会员充值页面
  goVipRecharge:function(){
    wx.navigateTo({
      url: '/'+this.data.adv_uri,
    })
  },
   // 保存图片
  saveImg: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    console.log(e);
    wx.downloadFile({
      url: _this.data.QRCodeImg,
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log(res);
            wx.showToast({
              title: '图片保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (res) {
            console.log(res)
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  // 预览图片
  previewImg: function () {
    var _this = this;
    var urls;
    if (_this.data.isUseHttp == 1) {
      urls = [_this.data.QRCodeImgHttp]
    } else {
      urls = [_this.data.QRCodeImg]
    }
    wx.previewImage({
      current: _this.data.QRCodeImg, // 当前显示图片的http链接
      urls: urls
    })
  },
  // 点击TAB切换按钮
  swichNav:function(e){
    this.setData({
      currentTab: e.currentTarget.dataset.current
    })
  },
  // 阻止点击事件
  preventEvent: function () {
  },
  // 关闭弹层
  closeMask: function () {
    this.setData({
      isShareMaskShow: 0
    })
  },
  // 点击发送粉丝福利按钮
  sendWelfare: function (e) {
    var formId = e.detail.formId;
    app.postFormId(this.data.token, formId);
    this.setData({
      isShareMaskShow: 1
    })
  },
  // 点击获取生成二维码按钮
  gerQrcode: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    _this.getQRCodeImg();
    _this.setData({
      isQRCodeMaskShow: 1,
      isShareMaskShow: 0
    })
  },
  // 关闭分享二维码弹层
  closeQRCodeMask: function () {
    this.setData({
      isQRCodeMaskShow: 0
    })
  },
  // 获取二维码图片
  getQRCodeImg: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type
    }
    app.post(getQRCodeImgUrl, requestData, function (res) {
      if(res.code==2000){
      _this.setData({
        QRCodeImg: res.data.image,
        QRCodeImgHttp: res.data.image_http
      })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  // 获取页面信息
  getPageInfo: function () {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    _this.setData({
      resourcesUrl: configs.static_url_http
    })
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
    }
    app.post(getPageInfoUrl, requestData, function (res) {
      if (res.code == 2000) {
        var resourcesUrl = _this.data.resourcesUrl;
        for (var i = 0; i < res.data.goods_list.length; i++) {
          res.data.goods_list[i].img = resourcesUrl + '/' + res.data.goods_list[i].img;
        }
        if (res.data.is_vip == 1) {
          var sharePage = res.data.share.page;
          var uid = sharePage.replace(/[^0-9]/ig, "")
          _this.setData({
            uid: uid
          })
        }
        _this.setData({
          isLoading:0,
          shareInfo: res.data.share,
          adv_uri: res.data.adv.adv_uri,
          adv_info_url: resourcesUrl+'/'+res.data.adv.adv_info_url,
          goodsList:res.data.goods_list,
          is_vip: res.data.is_vip,
          open_shop_invite_btn_title: res.data.open_shop_invite_btn_title,
          open_shop_table_right_title: res.data.open_shop_table_right_title,
          open_shop_table_left_title: res.data.open_shop_table_left_title,
          open_shop_one_left_title: res.data.open_shop_one_left_title,
          open_shop_one_right_title: res.data.open_shop_one_right_title,
          open_shop_two_no_vip_title: res.data.open_shop_two_no_vip_title,
          open_shop_two_vip_title: res.data.open_shop_two_vip_title,
          invite_list: res.data.invite_list
        })
       
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 获取列表信息
  getGoodsList:function(){
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      goods_page: _this.data.page
    }
    app.post(getGoodsListUrl, requestData, function (res) {
      if (res.code == 2000) {
        var resourcesUrl = _this.data.resourcesUrl;
        for (var i = 0; i < res.data.goods_list.length; i++) {
          res.data.goods_list[i].img = resourcesUrl + '/' + res.data.goods_list[i].img;
        }
        var goodslist = _this.data.goodsList;
        goodslist = goodslist.concat(res.data.goods_list);
        _this.setData({
          goodsList: goodslist,
          page: res.data.goods_page
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this= this;
  if(options.uid){
    _this.setData({
      uid: options.uid
    })
  }
    var scene = decodeURIComponent(options.scene);
    if (scene && scene !== 'undefined') {
      var sceneParams = new Array();
      sceneParams = scene.split('#');
      _this.setData({
        uid: sceneParams[0]
      })
    }
   
    wx.getSystemInfo({
      success: function (res) {
        if (res.system.search('iOS') == -1) {
          _this.setData({
            isUseHttp: 1
          })
        } else {
          _this.setData({
            isUseHttp: 0
          })
        }
        _this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    });
    app.checkAuthioze(success => {
      _this.loop();
    }, fail => {
      //未授权提示用户授权
      _this.setData({
        displayAuthShow: 1
      });
    });
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getPageInfo();
      
    }
  },
  /*角色权限控制*/
  getUserInfo: function (e) {
    var that = this;
    var infoUser = e.detail.userInfo;
    var res = e.detail;
    var url = app.setConfig.url + '/Login/doLogin';
    that.setData({
      displayAuthShow: 0,
      isGetInfo: 1
    });
    var data = {
      user_name: infoUser.nickName,
      nick_name: infoUser.nickName,
      head_img: infoUser.avatarUrl,
      sex: infoUser.gender,
      coutry: infoUser.country,
      city: infoUser.city,
      province: infoUser.province,
      code: app.globalData.code,
      encryptedData: res.encryptedData,
      signature: res.signature,
      ctype: app.setConfig.type,
      version: app.setConfig.version,
      iv: res.iv,
    }
    app.postLogin(url, data, function () {
      var cash = wx.getStorageSync('cash');
      if (cash != "") {
        that.setData({
          displayAuthShow: 0,
          isfriendShareMaskShow: 1,
          cash: cash
        })
      }
      that.loop();
    });
  },
  authRest: function () {
    this.setData({
      displayAuthShow: 0
    });
    setTimeout(function () {
      wx.reLaunch({
        url: '/pages/bargain/bargain',
      })
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loop();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentTab = this.data.currentTab;
    if (currentTab==1){
      this.getGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _this = this;
    var shareInfo = _this.data.shareInfo;
    _this.setData({
      isShareMaskShow: 0,
    })
    return {
      title: shareInfo.title,
      path: shareInfo.page,
      imageUrl: shareInfo.image
    }
  }
})